home *** CD-ROM | disk | FTP | other *** search
/ Suzy B Software 2 / Suzy B Software CD-ROM 2 (1994).iso / new_file / mintprgs / mint112s / mint112s.lzh / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-30  |  5.0 KB  |  316 lines

  1. /*
  2. Copyright 1990,1991,1992 Eric R. Smith.
  3. Copyright 1993,1994 Atari Corporation.
  4. All rights reserved.
  5. */
  6.  
  7. /* MiNT routines for doing console I/O */
  8.  
  9. #include "mint.h"
  10.  
  11. /*
  12.  * These routines are what Cconout, Cauxout, etc. ultimately call.
  13.  * They take an integer argument which is the user's file handle,
  14.  * and do the translation to a file ptr (and return an appropriate error
  15.  * message if necessary.
  16.  * "mode" may be RAW, COOKED, ECHO, or COOKED|ECHO.
  17.  * note that the user may not call them directly!
  18.  */
  19.  
  20. long
  21. file_instat(f)
  22.     FILEPTR *f;
  23. {
  24.     long r;
  25.  
  26.     if (!f) {
  27.         return EIHNDL;
  28.     }
  29.     r = 1;        /* default is to assume input waiting (e.g. TOS files)*/
  30.     if (is_terminal(f))
  31.         (void)tty_ioctl(f, FIONREAD, &r);
  32.     else
  33.         (void)(*f->dev->ioctl)(f, FIONREAD, &r);
  34.     return r;
  35. }
  36.  
  37. long
  38. file_outstat(f)
  39.     FILEPTR *f;
  40. {
  41.     long r;
  42.  
  43.     if (!f) {
  44.         return EIHNDL;
  45.     }
  46.     r = 1;        /* default is to assume output OK (e.g. TOS files) */
  47.     if (is_terminal(f))
  48.         (void)tty_ioctl(f, FIONWRITE, &r);
  49.     else
  50.         (void)(*f->dev->ioctl)(f, FIONWRITE, &r);
  51.     return r;
  52. }
  53.  
  54. long
  55. file_getchar(f, mode)
  56.     FILEPTR *f;
  57.     int mode;
  58. {
  59.     char c;
  60.     long r;
  61.  
  62.     if (!f) {
  63.         return EIHNDL;
  64.     }
  65.     if (is_terminal(f)) {
  66.         return tty_getchar(f, mode);
  67.     }
  68.     r = (*f->dev->read)(f, &c, 1L);
  69.     if (r != 1)
  70.         return MiNTEOF;
  71.     else
  72.         return ((long)c) & 0xff;
  73. }
  74.  
  75. long
  76. file_putchar(f, c, mode)
  77.     FILEPTR *f;
  78.     long c;
  79.     int mode;
  80. {
  81.     char ch;
  82.  
  83.     if (!f) {
  84.         return EIHNDL;
  85.     }
  86.     if (is_terminal(f)) {
  87.         return tty_putchar(f, c & 0x7fffffffL, mode);
  88.     }
  89.     ch = c & 0x00ff;
  90.     return (*f->dev->write)(f, &ch, 1L);
  91. }
  92.  
  93. /*
  94.  * OK, here are the GEMDOS console I/O routines
  95.  */
  96.  
  97. long ARGS_ON_STACK
  98. c_conin()
  99. {
  100.     return file_getchar(curproc->handle[0], COOKED|ECHO);
  101. }
  102.  
  103. long ARGS_ON_STACK
  104. c_conout(c)
  105.     int c;
  106. {
  107.     return file_putchar(curproc->handle[1], (long)c, COOKED);
  108. }
  109.  
  110. long ARGS_ON_STACK
  111. c_auxin()
  112. {
  113.     return file_getchar(curproc->handle[2], RAW);
  114. }
  115.  
  116. long ARGS_ON_STACK
  117. c_auxout(c)
  118.     int c;
  119. {
  120.     return file_putchar(curproc->handle[2], (long)c, RAW);
  121. }
  122.  
  123. long ARGS_ON_STACK
  124. c_prnout(c)
  125.     int c;
  126. {
  127.     return file_putchar(curproc->handle[3], (long)c, RAW);
  128. }
  129.  
  130. long ARGS_ON_STACK
  131. c_rawio(c)
  132.     int c;
  133. {
  134.     long r;
  135.     PROC *p = curproc;
  136.  
  137.     if (c == 0x00ff) {
  138.         if (!file_instat(p->handle[0]))
  139.             return 0;
  140.         r = file_getchar(p->handle[0], RAW);
  141.         if (r <= 0)
  142.             return 0;
  143.         return r;
  144.     }
  145.     else
  146.         return file_putchar(p->handle[1], (long)c, RAW);
  147. }
  148.  
  149. long ARGS_ON_STACK
  150. c_rawcin()
  151. {
  152.     return file_getchar(curproc->handle[0], RAW);
  153. }
  154.  
  155. long ARGS_ON_STACK
  156. c_necin()
  157. {
  158.     return file_getchar(curproc->handle[0],COOKED|NOECHO);
  159. }
  160.  
  161. long ARGS_ON_STACK
  162. c_conws(str)
  163.     const char *str;
  164. {
  165.     const char *p = str;
  166.     long cnt = 0;
  167.  
  168.     while (*p++) cnt++;
  169.     return f_write(1, cnt, str);
  170. }
  171.  
  172. long ARGS_ON_STACK
  173. c_conrs(buf)
  174.     char *buf;
  175. {
  176.     long size, r;
  177.     char *s;
  178.  
  179.     size = ((long)*buf) & 0xff;
  180.     r = f_read(0, size, buf+2);
  181.     if (r < 0) {
  182.         buf[1] = 0;
  183.         return r;
  184.     }
  185. /* if reading from a file, stop at first CR or LF encountered */
  186.     s = buf+2;
  187.     size = 0;
  188.     while(r-- > 0) {
  189.         if (*s == '\r' || *s == '\n')
  190.             break;
  191.         s++; size++;
  192.     }
  193.     buf[1] = (char)size;
  194.     return 0;
  195. }
  196.  
  197. long ARGS_ON_STACK
  198. c_conis()
  199. {
  200.     return -(!!file_instat(curproc->handle[0]));
  201. }
  202.  
  203. long ARGS_ON_STACK
  204. c_conos()
  205. {
  206.     return -(!!file_outstat(curproc->handle[1]));
  207. }
  208.  
  209. long ARGS_ON_STACK
  210. c_prnos()
  211. {
  212.     return -(!!file_outstat(curproc->handle[3]));
  213. }
  214.  
  215. long ARGS_ON_STACK
  216. c_auxis()
  217. {
  218.     return -(!!file_instat(curproc->handle[2]));
  219. }
  220.  
  221. long ARGS_ON_STACK
  222. c_auxos()
  223. {
  224.     return -(!!file_outstat(curproc->handle[2]));
  225. }
  226.  
  227. /* Extended GEMDOS routines */
  228.  
  229. long ARGS_ON_STACK
  230. f_instat(h)
  231.     int h;
  232. {
  233.     PROC *proc;
  234.     int fh = h;
  235.  
  236. #if O_GLOBAL
  237.     if (fh >= 100) {
  238.         proc = rootproc;
  239.         fh -= 100;
  240.     } else
  241. #endif
  242.         proc = curproc;
  243.  
  244.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  245.         DEBUG(("Finstat: bad handle %d", h));
  246.         return EIHNDL;
  247.     }
  248.     return file_instat(proc->handle[fh]);
  249. }
  250.  
  251. long ARGS_ON_STACK
  252. f_outstat(h)
  253.     int h;
  254. {
  255.     int fh = h;
  256.     PROC *proc;
  257. #if O_GLOBAL
  258.     if (fh >= 100) {
  259.         fh -= 100;
  260.         proc = rootproc;
  261.     } else
  262. #endif
  263.         proc = curproc;
  264.  
  265.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  266.         DEBUG(("Foutstat: bad handle %d", h));
  267.         return EIHNDL;
  268.     }
  269.     return file_outstat(proc->handle[fh]);
  270. }
  271.  
  272. long ARGS_ON_STACK
  273. f_getchar(h, mode)
  274.     int h, mode;
  275. {
  276.     int fh = h;
  277.     PROC *proc;
  278.  
  279. #if O_GLOBAL
  280.     if (fh >= 100) {
  281.         fh -= 100;
  282.         proc = rootproc;
  283.     } else
  284. #endif
  285.         proc = curproc;
  286.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  287.         DEBUG(("Fgetchar: bad handle %d", h));
  288.         return EIHNDL;
  289.     }
  290.     return file_getchar(proc->handle[fh], mode);
  291. }
  292.  
  293. long ARGS_ON_STACK
  294. f_putchar(h, c, mode)
  295.     int h;
  296.     long c;
  297.     int mode;
  298. {
  299.     int fh = h;
  300.     PROC *proc;
  301.  
  302. #if O_GLOBAL
  303.     if (fh >= 100) {
  304.         fh -= 100;
  305.         proc = rootproc;
  306.     } else
  307. #endif
  308.         proc = curproc;
  309.  
  310.     if (fh < MIN_HANDLE || fh >=MAX_OPEN) {
  311.         DEBUG(("Fputchar: bad handle %d", h));
  312.         return EIHNDL;
  313.     }
  314.     return file_putchar(proc->handle[fh], c, mode);
  315. }
  316.